#include <set>
#include <map>
#include <string>
#include <utility>
#include <iostream>
using std::cout;
using std::endl;
using std::set;
using std::map;
using std::pair;
using std::string;
using std::make_pair;


void test0(){
    set<int> nums;
    set<int> nums2{2,3,2,1,9,3,7,0,1};
    set<int> nums3 = nums2;
    set<int> nums4(nums2.begin(),nums2.end());

    //set不支持下标访问
    //nums2[0];
    
    //迭代器方式进行遍历
    set<int>::iterator it = nums2.begin();
    /* auto it = nums2.begin(); */
    for(;it != nums2.end();++it){
        cout << *it << " ";
    }
    cout << endl;

    for(auto & nu : nums2){
        cout << nu << " ";
    }
    cout << endl;

    //set的查找操作
    cout << nums2.count(9) << endl;
    cout << nums2.count(10) << endl;

    auto it2 = nums2.find(1);
    cout << *it2 << endl;

    it2 = nums2.find(8);
    if(it2 == nums2.end()){
        cout << "hello" << endl;
    }

    //set的插入操作
    //插入一个元素
    /* auto ret = nums2.insert(1); */
    pair<set<int>::iterator,bool> ret = nums2.insert(1);
    if(ret.second){
        cout << "插入成功" << endl;
        cout << *(ret.first) << endl;
    }else{
        cout << "插入失败，元素已存在" << endl;
        cout << *(ret.first) << endl;
    }


    //插入多个元素
    int arr[5] = {18,41,35,2,99};
    //传入首迭代器和尾后迭代器
    nums2.insert(arr,arr + 5); 

    for(auto & nu : nums2){
        cout << nu << " ";
    }
    cout << endl;

    //传入大括号的列表
    nums2.insert({100,1,111});
    for(auto & nu : nums2){
        cout << nu << " ";
    }
    cout << endl;

    auto it3 = nums2.begin();
    ++it3;
    ++it3;
    cout << *it3 << endl;
    /* *it3 = 300; */
}

void test1(){
    pair<int,string> nums = {1,"wangdao"};
    cout << nums.first << " " << nums.second << endl;
}

void checkFind(const map<int,string> & rhs,int key){
    auto it = rhs.find(key);
    if(it == rhs.end()){
        cout << "该元素不在map中" << endl;
    }else{
        cout << "该元素在map中" << endl;
        cout << it->first << " " << it->second << endl;
    }
}

void test2(){
	map<int,string> number = {
        {1,"hello"},
        {2,"world"},
        {3,"wangdao"},
        pair<int,string>(4,"hubei"),
        pair<int,string>(5,"wangdao"),
        make_pair(9,"shenzhen"),
        make_pair(3,"beijing"),
        make_pair(6,"shanghai")
    };

    //对map进行遍历
    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }

    cout << endl;
    /* auto it = number.begin(); */
    map<int,string>::iterator it = number.begin();
    for(; it != number.end(); ++it){
        /* cout << (*it).first << " " << (*it).second << endl; */
        cout << it->first << " " << it->second << endl;
    }

    //map中进行查找
    size_t cnt1 = number.count(1);
    size_t cnt2 = number.count(10);
    cout << cnt1 << endl;
    cout << cnt2 << endl;
    
    /* map<int,string>::iterator it2 = number.find(1); */
    /* auto it3 = number.find(10); */
    /* cout << "it2:" << it2->first << " " << it2->second << endl; */
    /* cout << "it3:" << it3->first << " " << it3->second << endl; */
    checkFind(number,10);
    cout << endl;
    checkFind(number,5);

    cout << "############" << endl;
    /* auto ret = number.insert(pair<int,string>(7,"nanjing")); */
    pair<map<int,string>::iterator,bool> ret = 
        number.insert(pair<int,string>(7,"nanjing"));
    if(ret.second){
        cout << "该元素插入成功" << endl;
        cout << ret.first->first << " " << ret.first->second << endl;
    }else{
        cout << "插入失败" << endl;
    }

    cout << endl;
    number.insert({9,"hangzhou"});
    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }

    cout << endl;
    //再创建一个map
	map<int,string> number2 = {{1,"beijing"},{18,"shanghai"}};

    //迭代器方式
    number2.insert(number.begin(),number.end());
    for(auto & nu : number2){
        cout << nu.first << " " << nu.second  << endl;
    }
  
    cout << endl;
    //列表方式
    cout << endl;
    number2.insert({{4,"guangzhou"},{8,"hello"}});
    for(auto & nu : number2){
        cout << nu.first << " " << nu.second  << endl;
    }
}

void test3(){
    //map的下标操作
	map<int,string> number = {
        {1,"hello"},
        {2,"world"},
        {3,"wangdao"},
        pair<int,string>(4,"hubei"),
        pair<int,string>(5,"wangdao"),    
        };

    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }
    //下标中的是key值，并非传统的下标
    cout << number[1] << endl;

    cout << endl;
    //支持写操作
    number[3] = "cpp";
    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }
    //如果下标对应的key不存在，
    //会插入一个元素进入map
    //value按默认方式构造
    cout << number[6] << endl;

    cout << endl;
    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }

    number[7] = "hubei";
    for(auto & nu : number){
        cout << nu.first << " " << nu.second  << endl;
    }

}


int main(void){
    test3();
    return 0;
}
